Python
R
Advance Computing
- Machine Learning
- Machine Vision
- Data Scientist
In [ ]:
In [1]:
from array import array
In [2]:
array('i', [1, 2, 3])
Out[2]:
In [ ]:
In [3]:
import numpy as np
In [4]:
np.array([1, 5, 6, 9])
Out[4]:
In [5]:
arr = np.array([1, 5, 6, 9])
In [6]:
arr.dtype
Out[6]:
In [7]:
np.array([1.2, 5.6, 4, 9.0, 7])
Out[7]:
In [8]:
np.array([1.2, 5.6, 4, 9.0, 7]).dtype
Out[8]:
In [10]:
np.array(['1', 5, 6])
Out[10]:
In [11]:
np.arange(1, 9)
Out[11]:
In [12]:
m1 = np.arange(1, 9)
In [13]:
m1
Out[13]:
In [14]:
m1.shape
Out[14]:
In [16]:
m1.size
Out[16]:
In [19]:
m1 * 4
Out[19]:
In [21]:
m1* 2
Out[21]:
In [22]:
m1 + (m1 * 2)
Out[22]:
In [23]:
m1.ndim
Out[23]:
In [24]:
m2 = np.array([[1, 2, 3], [7, 8, 9]])
In [25]:
m2.ndim
Out[25]:
In [26]:
m2.size
Out[26]:
In [27]:
m2.shape
Out[27]:
In [28]:
m2
Out[28]:
In [31]:
m3 = m2.transpose()
In [32]:
m3
Out[32]:
In [33]:
m3.shape
Out[33]:
In [34]:
np.zeros((2, 3))
Out[34]:
In [35]:
np.ones((3, 2))
Out[35]:
In [36]:
np.diag((3, 4))
Out[36]:
In [39]:
help(np.ones)
In [40]:
np.ones(5)
Out[40]:
In [41]:
np.diag(np.ones(5))
Out[41]:
In [44]:
np.linspace(0, 1)
Out[44]:
In [87]:
x = np.linspace(1, 10, num=20)
In [88]:
x
Out[88]:
In [89]:
x[1] - x[0]
Out[89]:
In [45]:
np.linspace(1, 99)
Out[45]:
In [46]:
np.linspace(1, 5, num=20)
Out[46]:
In [47]:
%matplotlib inline
In [48]:
import matplotlib.pyplot as plt
In [49]:
plt.plot(np.linspace(0, 1))
Out[49]:
In [50]:
np.pi
Out[50]:
In [52]:
np.sin(np.pi/2.0)
Out[52]:
In [90]:
np.sin(1.4)
Out[90]:
In [93]:
np.sin(np.array([1.4, 2, 3]))
Out[93]:
In [53]:
x_range = np.linspace(-np.pi, np.pi, 50)
In [54]:
x_range
Out[54]:
In [56]:
np.sin(x_range)
Out[56]:
In [55]:
plt.plot(x_range, np.sin(x_range))
Out[55]:
In [57]:
plt.plot(x_range, np.tan(x_range))
Out[57]:
In [ ]:
In [61]:
np.random.rand(3, 2)
Out[61]:
In [63]:
np.random.randint(1, 99)
Out[63]:
In [64]:
np.random.randint(1, 99)
Out[64]:
In [66]:
np.random.randint(1, 99, size=8)
Out[66]:
In [67]:
m5 = np.random.randint(1, 99, size=12)
In [68]:
m5
Out[68]:
In [69]:
m5.mean()
Out[69]:
In [70]:
np.median(m5)
Out[70]:
In [71]:
m5.max()
Out[71]:
In [72]:
m5.min()
Out[72]:
In [73]:
m5.std()
Out[73]:
In [74]:
m5.sum()
Out[74]:
In [ ]:
In [75]:
import pandas as pd
In [76]:
countries = ["Nepal", "India", "Pakistan", "Bhutan"]
zip_codes = [977, 91, 233, 987]
In [79]:
dataset = list(zip(countries, zip_codes))
In [80]:
dataset
Out[80]:
In [81]:
df = pd.DataFrame(data=dataset, columns=["Country", "Zip Code"])
In [82]:
df
Out[82]:
In [ ]:
In [97]:
dframe = pd.read_csv('API_NPL_DS2_en_csv_v2.csv', skiprows=3)
In [99]:
dframe.head()
Out[99]:
In [100]:
dframe.tail()
Out[100]:
In [101]:
dframe.dtypes
Out[101]:
In [111]:
dframe["2014"]
Out[111]:
In [108]:
dframe[["Indicator Name", "2014"]].head()
Out[108]:
In [110]:
dframe[dframe["Indicator Name"] == "Agricultural land (sq. km)"]
Out[110]:
In [112]:
agri = dframe[dframe["Indicator Name"] == "Agricultural land (sq. km)"]
In [114]:
agri.plot(kind='bar')
Out[114]:
In [129]:
agri.loc[0:, "2000":"2013"]
Out[129]:
In [133]:
agri.loc[0:, "2000":"2013"].plot(kind='bar')
Out[133]:
In [138]:
agri = agri.reset_index()
In [139]:
agri.loc[0:, "2000":"2013"]
Out[139]:
In [140]:
agri.loc[0:, "2000":"2013"].transpose()
Out[140]:
In [141]:
agri.loc[0:, "2000":"2013"].transpose().plot(kind='bar')
Out[141]:
In [142]:
agri_t = agri.loc[0:, "2000":"2013"].transpose()
In [143]:
agri_t
Out[143]:
In [ ]:
In [ ]:
In [144]:
agri_t.columns = ["Agricultural land (sq. km)"]
In [145]:
agri_t
Out[145]:
In [146]:
agri_t.plot(kind="bar")
Out[146]:
In [147]:
agri_t.plot()
Out[147]:
In [155]:
dframe[dframe["Indicator Name"].str.contains('of goods and services')]
Out[155]:
In [173]:
import_export = dframe[dframe["Indicator Code"].isin(["BM.GSR.GNFS.CD",
"BX.GSR.GNFS.CD"])]
In [174]:
import_export
Out[174]:
In [175]:
import_export = import_export.loc[0:, "2007":"2014"]
In [176]:
import_export
Out[176]:
In [177]:
import_export = import_export.transpose()
In [178]:
import_export
Out[178]:
In [179]:
import_export.columns = ["Imports of goods and services (BoP, current US$)",
"Exports of goods and services (BoP, current US$)"]
In [180]:
import_export
Out[180]:
In [181]:
import_export.plot()
Out[181]:
In [184]:
import_export.apply(pd.to_numeric, args=('coerce',))
Out[184]:
In [ ]:
In [185]:
names = ['Bob', 'Jessica', 'Hari', 'John', 'Rajesh', 'Seldon']
In [207]:
names[np.random.randint(0, len(names))]
Out[207]:
In [208]:
random_names = [names[np.random.randint(0, len(names))]
for i in range(0, 99)]
In [210]:
len(random_names)
Out[210]:
In [211]:
random_ages = [np.random.randint(10, 78) for i in range(0, 99)]
In [213]:
len(random_ages)
Out[213]:
In [215]:
age_distrib = pd.DataFrame(list(zip(random_names, random_ages)),
columns=["Name", "Age"])
In [216]:
age_distrib.head()
Out[216]:
In [225]:
bob.count()
Out[225]:
In [229]:
bob = age_distrib[age_distrib["Name"] == "Bob"]
bob
Out[229]:
In [230]:
bob = bob.reset_index()
In [231]:
bob
Out[231]:
In [235]:
bob.plot(kind="scatter", x='index', y='Age')
Out[235]:
In [238]:
np.unique(age_distrib["Name"], return_inverse=True)
Out[238]:
In [239]:
age_distrib["Name"]
Out[239]:
In [241]:
np.unique(age_distrib["Name"], return_inverse=True)
Out[241]:
In [271]:
unique_names, x_values = np.unique(age_distrib["Name"],
return_inverse=True)
In [243]:
unique_names
Out[243]:
In [274]:
x_values = x_values + 1
In [275]:
age_distrib["Values"] = x_values
In [276]:
age_distrib
Out[276]:
In [247]:
unique_ages, y_values = np.unique(age_distrib["Age"],
return_inverse=True)
In [251]:
len(x_values)
Out[251]:
In [277]:
age_distrib.plot(kind='scatter', x='Values', y='Age')
Out[277]:
In [284]:
unique_names = np.insert(unique_names, 0, '')
In [285]:
unique_names
Out[285]:
In [286]:
ax = age_distrib.plot(kind='scatter', x='Values', y='Age',
color='cyan')
ax.set_xticklabels(unique_names)
ax
Out[286]:
In [304]:
age_distrib[["Name", "Age"]].head()
Out[304]:
In [307]:
age_distrib[["Name", "Age"]].groupby('Name')
Out[307]:
In [301]:
people_count = age_distrib[["Name", "Age"]].groupby('Name').count()
In [302]:
people_count
Out[302]:
In [303]:
people_count.plot(kind='pie', y='Age')
Out[303]: